package cn.qinhao.pethome.employee.controller;

import cn.qinhao.pethome.basic.until.AjaxResult;
import cn.qinhao.pethome.basic.until.PageList;
import cn.qinhao.pethome.employee.domain.Employee;
import cn.qinhao.pethome.employee.query.EmployeeQuery;
import cn.qinhao.pethome.employee.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Resource
    private IEmployeeService employeeService;
    //查询所有数据
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    public List<Employee> loadAll(){
        return employeeService.selectAll();
    }
    //查询一条数据
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee loadOne(@PathVariable("id") Long id){
        return employeeService.selectOne(id);
    }
    //增加或者修改
    @PutMapping
    @ApiOperation(value = "新增或修改操作" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee ){
        try {
            if (employee.getId()==null){
                employeeService.add(employee);
                return AjaxResult.me().setSuccess(true).setMessage("添加成功");
            }else{
                employeeService.update(employee);
                return AjaxResult.me().setSuccess(true).setMessage("修改成功");
            }
        }catch (Exception e ){
            return AjaxResult .me() .setSuccess(false ).setMessage("操作失败");
        }
    }
    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult  delete(@PathVariable("id") Long id){
        try{
            employeeService.delete(id);
            return AjaxResult.me().setSuccess(true).setMessage("删除成功");
        }catch (Exception e ){
            return AjaxResult .me() .setSuccess(false ).setMessage("删除失败");
        }
    }
    //批量删除
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try{
            employeeService.batchDelete(ids);
            return AjaxResult.me().setSuccess(true).setMessage("批量删除成功");
        }catch (Exception e ){
            return AjaxResult .me() .setSuccess(false ).setMessage("网络繁忙");
        }
    }
    //分页
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return  employeeService.pageQuery(query);
    }
}
